package com.jrx.anytxn.param.controller.api;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.dto.investor.HandleInvestorLimitReq;
import com.jrx.anytxn.param.dto.investor.HandleInvestorLimitRes;
import com.jrx.anytxn.param.service.investor.IPrInvestorTableService;
import com.jrx.anytxn.redisson.service.RedissonLockService;
import com.jrx.anytxn.redisson.util.LockType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * 出资方api接口
 *
 * @author 刘杨
 * @date 2020-07-10
 **/
@Api(tags = "出资方api接口")
@RestController
@RequestMapping(value = "/api/param/investor")
public class InvestorController {

    private static final Logger logger = LoggerFactory.getLogger(InvestorController.class);

    @Resource
    private IPrInvestorTableService iPrInvestorTableService;
    @Autowired
    private RedissonLockService redissonLockService;

    @ApiOperation(value = "出资方额度使用", notes = "出资方额度使用")
    @PostMapping("/handleInvestorLimit")
    public TxnRespResult<HandleInvestorLimitRes> handleInvestorLimit(@Valid @RequestBody HandleInvestorLimitReq req) throws TxnException {
        String lockKey = String.format(Constant.INVESTOR_REDIS_LOCK, req.getTenantId(), req.getInvestorId());
        RLock lock = redissonLockService.getRLock(lockKey, LockType.REENTRANT_LOCK);

        try {
            logger.info("出资方额度使用处理开始：{}", req.toString());
            // 获取redis锁
            boolean lockFlag = lock.tryLock(Constant.INVESTOR_REDIS_LOCK_WAITE_TIME, Constant.INVESTOR_REDIS_LOCK_LESS_TIME, TimeUnit.SECONDS);
            if (!lockFlag) {
                logger.error(TxnRespCode.TRANSACTION_PROCESSING.getMsg());
                throw new TxnBizException(TxnRespCode.TRANSACTION_PROCESSING.getCode(), TxnRespCode.TRANSACTION_PROCESSING.getMsg());
            }
            TxnRespResult<HandleInvestorLimitRes> result = new TxnRespResult<>();
            HandleInvestorLimitRes res = iPrInvestorTableService.handleInvestorLimit(req);
            result = result.getSuccess(res);
            logger.info("出资方额度使用处理结束：{}", result.toString());
            return result;
        } catch (InterruptedException e) {
            String msg = String.format(Constant.GET_REDIS_LOCK_ERROR, lockKey);
            logger.error("异常信息{}", e);
            throw new TxnException(msg);
        } finally {
            lock.unlock();
        }
    }
}
